import tkinter as tk
from tkinter import messagebox, scrolledtext
from alg2 import WordLadderSolver
import random

class WordLadderUI:
    def __init__(self, root):
        self.root = root
        self.root.title("单词转换序列查找器")
        
        # 预设单词库 (确保有有效的转换路径)
        self.word_pool = [
            "hit", "hot", "dot", "dog", "lot", "log", "cog",
            "cat", "cot", "cut", "but", "put", "pat", "pot",
            "red", "ted", "bed", "bet", "let", "leg", "peg"
        ]
        
        # 自动生成按钮
        self.generate_button = tk.Button(root, text="自动生成测试数据", command=self.generate_test_data)
        self.generate_button.grid(row=0, column=0, columnspan=2, pady=5)
        
        # 显示生成的单词
        self.begin_word_label = tk.Label(root, text="起始单词:")
        self.begin_word_label.grid(row=1, column=0, padx=5, pady=5, sticky="e")
        self.begin_word_display = tk.Label(root, text="", relief="sunken", width=15)
        self.begin_word_display.grid(row=1, column=1, padx=5, pady=5, sticky="w")
        
        self.end_word_label = tk.Label(root, text="目标单词:")
        self.end_word_label.grid(row=2, column=0, padx=5, pady=5, sticky="e")
        self.end_word_display = tk.Label(root, text="", relief="sunken", width=15)
        self.end_word_display.grid(row=2, column=1, padx=5, pady=5, sticky="w")
        
        self.word_list_label = tk.Label(root, text="单词列表:")
        self.word_list_label.grid(row=3, column=0, padx=5, pady=5, sticky="ne")
        self.word_list_display = scrolledtext.ScrolledText(root, width=40, height=5, wrap=tk.WORD)
        self.word_list_display.grid(row=3, column=1, padx=5, pady=5)
        
        # 查找按钮
        self.find_button = tk.Button(root, text="查找最短转换序列", command=self.find_ladders)
        self.find_button.grid(row=4, column=0, columnspan=2, pady=10)
        
        # 结果显示区域
        self.result_label = tk.Label(root, text="转换结果:")
        self.result_label.grid(row=5, column=0, padx=5, pady=5, sticky="ne")
        self.result_text = scrolledtext.ScrolledText(root, width=50, height=9, wrap=tk.WORD)
        self.result_text.grid(row=5, column=1, padx=5, pady=5)
        
        # 初始生成一组数据
        self.generate_test_data()
    
    def generate_test_data(self):
        # 确保beginWord和endWord不同且有转换路径
        while True:
            begin_word = random.choice(self.word_pool)
            end_word = random.choice(self.word_pool)
            if begin_word != end_word:
                break
        
        # 随机选择5-8个单词作为wordList (包含beginWord和endWord)
        word_list = random.sample(self.word_pool, random.randint(5, 8))
        if begin_word not in word_list:
            word_list.append(begin_word)
        if end_word not in word_list:
            word_list.append(end_word)
        
        # 更新显示
        self.begin_word_display.config(text=begin_word)
        self.end_word_display.config(text=end_word)
        self.word_list_display.delete(1.0, tk.END)
        self.word_list_display.insert(tk.END, ", ".join(word_list))
        
        # 清空结果
        self.result_text.delete(1.0, tk.END)
        
        # 保存当前数据
        self.current_data = {
            "begin_word": begin_word,
            "end_word": end_word,
            "word_list": word_list
        }
    
    def find_ladders(self):
        if not hasattr(self, "current_data"):
            messagebox.showerror("错误", "请先生成测试数据")
            return
        
        begin_word = self.current_data["begin_word"]
        end_word = self.current_data["end_word"]
        word_list = self.current_data["word_list"]
        
        # 调用算法类的方法
        result = WordLadderSolver.find_ladders(begin_word, end_word, word_list)
        
        # 显示结果
        self.result_text.delete(1.0, tk.END)
        if not result:
            self.result_text.insert(tk.END, f"没有找到从 '{begin_word}' 到 '{end_word}' 的转换序列")
        else:
            self.result_text.insert(tk.END, f"找到 {len(result)} 条最短转换序列:\n\n")
            for i, path in enumerate(result, 1):
                self.result_text.insert(tk.END, f"路径 {i}: {' -> '.join(path)}\n\n")

def main():
    root = tk.Tk()
    root.resizable(False, False)
    app = WordLadderUI(root)
    root.mainloop()

if __name__ == "__main__":
    main()
